<?php

class UserScoresHistory extends Eloquent {

    protected $table = 'user_scores_history';

    const SCORE_UNIQUE_EVENT_REGISTRATION = 1;
    const SCORE_UNIQUE_EVENT_VERIFY = 2;

    public static function registration(User $user)
    {
        self::_store_scores_by_events($user, self::SCORE_UNIQUE_EVENT_REGISTRATION, 'Registration', 100);
        if ($user->invitor){
        	$invitor = User::where('id',$user->invitor)->first();
        	self::inviteOther($invitor);
        }
    }

    public static function post()
    {
        if (Auth::user())
        {
            self::_store_scores_by_events(NULL, NULL, 'Post', 5);
        }
    }

    public static function comment()
    {
        self::_store_scores_by_events(NULL, NULL, 'Comment', 5);
    }

    public static function like(User $user)
    {
        self::_store_scores_by_events($user, NULL, 'Like', 2);
    }

    public static function collect(User $user)
    {
        self::_store_scores_by_events($user, NULL, 'Collect', 2);
    }
    
    public static function inviteOther(User $user)
    {
    	self::_store_scores_by_events($user,null, 'inviteOther', 20);
    }
    
    public static function shareOK(User $user)
    {
    	self::_store_scores_by_events($user,null, 'shareOK', 5);
    }

	public static function verifyLawer(User $user)
	{
    	self::_store_scores_by_events($user, self::SCORE_UNIQUE_EVENT_VERIFY, 'verifyOK', 20);
	}

    private static function _store_scores_by_events($user, $unique_event, $action, $score)
    {
        if ($user == NULL)
        {
            $user = Auth::user();
        }
        $operator = $user;

        $history = self::_create_score_history($user, $operator, $unique_event, $action, $score);

        DB::transaction(function() use($user, $unique_event, $score, $history){
            if ($unique_event AND UserScoresHistory::where('user_id', '=', $user->id)->where('unique_event', '=', $unique_event)->lockForUpdate()->count() > 0)
            {
                return;
            }
            if ($score > 0)
            {
                $user->increment('score', $score);
            }
            else
            {
                $user->decrement('score', $score);
            }

            $user->save();
            $history->save();
        });
    }

    private static function _create_score_history(User $user, User $operator, $unique_event, $action, $score)
    {
        $history = new UserScoresHistory();
        $history->user_id = $user->id;
        $history->unique_event = $unique_event;
        $history->action = $action;
        $history->score = $score;
        $history->ip = Request::getClientIp();
        $history->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'NULL';
        $history->request_url = Request::url();
        $history->operator_id = $operator->id;

        return $history;
    }

	public static function getScoreHistorys($limit, $offset, $order_by = 'created_at')
    {
        return UserScoresHistory::where('user_id', '=', Auth::user()->id)
            ->take($limit)
            ->skip($offset)
            ->orderBy($order_by, 'DESC')
            ->get();
    }



}